package com.lzx.adx.report.support.strategy.v2.channel;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
@ProductType(ProductChannelConstant.YEIYU)
public class YeYuReportStrategy extends AbstractReport{

    @Override
    public String getReportFinalUrl(ReportDTO reportDTO, AccountDTO accountDTO) {

        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(reportDTO.getOaid()) && !StringUtils.contains(reportDTO.getOaid(), "__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !StringUtils.contains(reportDTO.getOaidSum(), "__")){
            params.put("oaid_md5", reportDTO.getOaidSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !StringUtils.contains(reportDTO.getImeiSum(), "__")){
            params.put("imei", reportDTO.getImeiSum());
        }
        //国际移动设备标识符 明⽂imei或者md5加密imei (会根据⻓度判断等于32时为md5加密后的值) 能获取到，则必填
        if(StringUtils.isNotBlank(reportDTO.getImei()) && !StringUtils.contains(reportDTO.getImei(), "__")){
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getHash_android_id()) && !StringUtils.contains(reportDTO.getHash_android_id(), "__")){
            params.put("android_id", reportDTO.getHash_android_id());
        }
        params.put("ua", StringUtils.isNotBlank(reportDTO.getUser_agent()) ? URLUtil.encode(reportDTO.getUser_agent()) : "");
        params.put("ip", StringUtils.isNotBlank(reportDTO.getIp()) ? reportDTO.getIp() : "");
        params.put("os", "android");
        params.put("clickid", reportDTO.getBizRequestId());
        String callbackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        String urlParams = UrlUtil.covertParamsOrderByAsc(params) + "&callback=" + URLEncoderDecoderTool.encodeURL(callbackUrl);
        return accountDTO.getDspReportUrl() + "&" + urlParams;
    }

}
